package com.danger.Model.action;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.util.Log;
import com.danger.Model.bean.VideoBean;
import com.danger.Model.listener.OnCommonListener;
import com.danger.Model.listener.OnExtractAudioListener;
import com.danger.Model.listener.OnGetThumbListener;
import com.danger.Util.FileCacheService;
import org.xutils.common.Callback;
import org.xutils.ex.HttpException;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.io.File;

/**
 * Created by danger on 2016/4/7.
 */
public class ExtractAudioModelImpl implements ExtractAudioModel {

    private Context context;
    private FileCacheService service;
    private static final String key = "5719499bc59080223b4ea8ca3882929b";
    private String savePath;

    public ExtractAudioModelImpl(Context context) {
        this.context = context;
        service = new FileCacheService(context);
    }

    @Override
    public void getAudio(VideoBean videoBean, OnExtractAudioListener listener) {

    }


    @Override
    public void getThumb(String path, OnGetThumbListener listener) {
        Log.i("TAG", "model_path------>>" + path);
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        File file = new File(path);
        if (file.exists()) {
            mmr.setDataSource(file.getAbsolutePath());
            Bitmap bitmap = mmr.getFrameAtTime();//获得视频第一帧的Bitmap对象
            if (bitmap != null) {
                listener.onSuccess(bitmap, "获取视频缩略图成功");
            } else {
                listener.onError("获取视频缩略图失败");
            }
        } else {
            listener.onError("文件不存在");
        }
    }

    @Override
    public void getContentByNet(String path, String rate, String type, final OnCommonListener listener) {
        RequestParams params = new RequestParams("http://172.21.4.81:8080/MyServlet/servlet/AudioServlet");
        Log.i("TAG", path);
        params.addBodyParameter("file", new File(path));
        params.addBodyParameter("rate", rate);
        Callback.Cancelable cancelable = x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {

                    listener.onSuccess(result);
                    Log.i("TAG", "audio_content----->>" + result);
                } catch (Exception e) {
                    listener.onError("JSON解析异常");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (ex instanceof HttpException) { // 网络错误
                    HttpException httpEx = (HttpException) ex;
                    String errorResult = httpEx.getResult();
                    Log.i("TAG", "onError--->>" + errorResult);
                    listener.onError(errorResult);
                } else { // 其他错误
                    listener.onError("其他错误");
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {
                listener.onError("已取消请求");
            }

            @Override
            public void onFinished() {

            }
        });
    }
}